import matplotlib.pyplot as plt
import torch
import numpy as np

ious = []
scores = []

# 读取文件
with open('/home/cc/AAA_python_test/IOU&Cls/combined_output_QFL2.txt', 'r') as f:
    for line in f:
        if line.strip() and not line.startswith('tensor([]'):
            iou_str, score_str = line.strip().split(",")
            # iou = torch.tensor(eval(iou_str.split('tensor(')[1].strip(')')))
            # score = torch.tensor(eval(score_str.strip(')')))
            iou = torch.tensor([eval(iou_str)])  # 将 iou 转换为包含单个元素的列表
            score = torch.tensor([eval(score_str)])  # 将 score 转换为包含单个元素的列表
            ious.extend(iou.tolist())
            scores.extend(score.tolist())

# 绘制图形
plt.scatter(scores, ious, alpha=0.01,s=10)

plt.title('IOU vs Classification Score QFL2')
plt.xlabel('Classification Score')
plt.ylabel('IOU')
plt.show()
#############################################################
# 绘制图形
# 画一条对角线
plt.plot([0, 0.9], [0, 1], 'r--')
plt.xlim(0, 1)
plt.ylim(0, 1)
plt.scatter(scores, ious, alpha=0.01,s=10)

plt.title('IOU vs Classification Score QFL2')
plt.xlabel('Classification Score')
plt.ylabel('IOU')
plt.show()
#############################################################
# 将数据转换为numpy数组
ious = np.array(ious)
scores = np.array(scores)
#############################################################
# 绘制二维直方图
plt.hist2d(scores, ious, bins=50, cmap='jet')
plt.title('IOU vs Classification Score QFL2')
plt.xlabel('Classification Score')
plt.ylabel('IOU')

cbar = plt.colorbar()
cbar.set_label('Count')
plt.show()
#############################################################

# 绘制二维直方图

# 我想通过改变数组的方式来排除掉x y <0 的数据
mask = (scores > 0) & (ious > 0)
scores = scores[mask]
ious = ious[mask]

plt.hist2d(scores, ious, bins=50, cmap='jet')
plt.title('IOU vs Classification Score QFL2')
plt.xlabel('Classification Score')
plt.ylabel('IOU')
# 画一条对角线
plt.plot([0, 0.9], [0, 1], 'r--')

plt.xlim(0, 1)
plt.ylim(0, 1)
cbar = plt.colorbar()
cbar.set_label('Count')
plt.show()


